<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <Upload :my-refresh="fetchData"></Upload>
        <el-button icon="el-icon-scissors" type="primary" @click="selectToCUt">
          固定切分
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              placeholder="请输入视频名"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :height="height"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="name"
        label="视频名称"
      ></el-table-column>
      <el-table-column show-overflow-tooltip prop="size" label="视频大小">
        <template slot-scope="scope">{{ scope.row.size }}M</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="duration" label="视频时长">
        <template slot-scope="scope">{{ scope.row.duration }}s</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="fileUrl" label="视频链接">
        <template #default="{ row }">
          <IndexPlay :title="row.name" :url="row.fileUrl"></IndexPlay>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createUserName"
        label="上传者"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createTime"
        label="上传时间"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-row>
            <el-col :span="8">
              <Player
                :id="row.id"
                :url="row.fileUrl"
                :title="row.name"
              ></Player>
            </el-col>
            <el-col :span="8">
              <el-button
                :disabled="!row.segmentState"
                type="text"
                @click="toDetail(row)"
              >
                详情
              </el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <el-dialog
      title="固定时长切分"
      :visible.sync="cutDialog"
      width="35%"
      :close-on-click-modal="false"
    >
      <br />
      <el-form
        ref="BrandsForm"
        v-loading="cutload"
        status-icon
        label-width="120px"
      >
        <el-form-item label="输入切分时长：" prop="logo">
          <el-input
            v-model.trim="cuttime"
            placeholder="请输入切分时长(以秒为单位)"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitCut">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import { doCut, doDelete, getList } from '@/api/videos'
  import Edit from './components/UserManagementEdit'
  import Upload from '../../vab/upload/index'
  import Player from './components/cutPlay'
  import IndexPlay from './components/indexplay'
  export default {
    name: 'Videos',
    components: { Edit, Player, Upload, IndexPlay },
    data() {
      return {
        list: [],
        cuttime: '',
        cutload: false,
        cutDialog: false,
        listLoading: true,
        background: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          name: '',
          pageNo: 1,
          pageSize: 10,
        },
        cutid: {
          ids: [],
          duration: 0,
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      toDetail(val) {
        this.$router.push({ path: '/videoDetail', query: { id: val.id } })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      async submitCut() {
        this.cutload = true
        this.cutid.duration = Number(this.cuttime)
        const result = await doCut(this.cutid)
        if (result.data.success) {
          this.$message({
            message: '切分成功！',
            type: 'success',
          })
          this.cutDialog = false
          await this.fetchData()
        } else {
          this.$message({
            message: '切分失败,先去看看是否进行过切分吧！',
            type: 'error',
          })
        }
        this.cutload = false
      },
      selectToCUt() {
        if (this.selectRows.length) {
          this.cutDialog = true
          this.cutid.durations = 0
          const ids_join = this.selectRows.map((item) => item.id).join()
          let ids = []
          let temp = ''
          for (let i = 0; i < ids_join.length; i++) {
            if (ids_join[i] !== ',') temp = temp + ids_join[i]
            else {
              ids.push(Number(temp))
              temp = ''
            }
          }
          ids.push(Number(temp))
          this.cutid.ids = ids
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleDelete() {
        if (this.selectRows.length) {
          const ids_join = this.selectRows.map((item) => item.id).join()
          let paykey = {
            ids: [],
          }
          let temp = ''
          for (let i = 0; i < ids_join.length; i++) {
            if (ids_join[i] !== ',') temp = temp + ids_join[i]
            else {
              paykey.ids.push(Number(temp))
              temp = ''
            }
          }
          paykey.ids.push(Number(temp))
          this.$baseConfirm('你确定要删除选中项吗', null, async () => {
            const { msg } = await doDelete(paykey)
            this.$baseMessage('删除成功！', 'success')
            await this.fetchData()
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const query = await getList(this.queryForm)
        let data = query.data
        let temp = data.data
        for (let i = 0; i < temp.length; i++) {
          temp[i].size = (Number(temp[i].size) / 1024 / 1024).toFixed(2)
        }
        this.list = temp
        this.total = data.totalResults
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
      test() {
        console.log('父组件')
      },
    },
  }
</script>
